package root.electronclass.electronclass.grade.homework

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import root.electronclass.electronclass.CommonActivity
import root.electronclass.electronclass.R
import root.electronclass.electronclass.adapter.GridSpacingItemDecoration
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.BasicDialogFragment
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentClassHomeworkBinding
import root.electronclass.electronclass.databinding.FragmentClassHomeworkItemBinding
import root.electronclass.electronclass.databinding.FragmentClassRotaHeaderBinding
import root.electronclass.electronclass.ext.doOnSearch
import root.electronclass.electronclass.ext.onResult
import root.electronclass.electronclass.ext.startFragment
import root.electronclass.electronclass.ext.toMMdd
import root.electronclass.electronclass.grade.homework.viewmodel.ClassHomeworkViewModel
import root.electronclass.electronclass.grade.model.ClassCurriculumItem
import root.electronclass.electronclass.grade.model.ClassHomeworkItem
import root.electronclass.electronclass.grade.viewmodel.WeekType
import root.electronclass.electronclass.other.AttestationActivity

/**
 * 班级作业
 * @author Lokiy
 * @date 2023/2/18 17:29
 */
class ClassHomeworkFragment : ViewBindingFragment<FragmentClassHomeworkBinding>() {

    private val viewModel: ClassHomeworkViewModel by viewModels()
    private var currentWeekType = WeekType.CURRENT.type

    @SuppressLint("NotifyDataSetChanged")
    private val titleAdapter = BasicAdapter<ClassCurriculumItem, FragmentClassRotaHeaderBinding> { holder, item ->
        holder.binding.itemName.text = item.subject.replace("今天", "")
        holder.binding.itemTime.text = item.startTime.toMMdd
        holder.binding.itemToday.isVisible = item.subject.contains("今天")

//        holder.binding.root.isActivated = holder.binding.itemTime.text.toString() != viewModel.selectDate.value && item.subject.contains("今天")
        holder.binding.root.isSelected = item.startTime == viewModel.selectDate.value
        holder.binding.root.setOnClickListener {
            notifyDataSetChanged()
            viewModel.loadData(date = item.startTime)
        }
    }
    private val adapter = BasicAdapter<ClassHomeworkItem, FragmentClassHomeworkItemBinding> { holder, item ->
        holder.binding.itemName.text = item.name
        holder.binding.itemSubjectShort.text = item.subject?.take(1)
        holder.binding.itemSubjectName.text = getString(R.string.class_homework_subject, item.subject)
        holder.binding.itemPublisher.text = getString(R.string.class_homework_publisher, item.creator)
        val text = binding.search.text.trim().toString()
        holder.binding.itemBg.isSelected = text == item.name || text == item.subject
        holder.binding.root.setOnClickListener {
            ClassHomeworkDetailDialog().also {
                it.arguments = bundleOf(
                    BasicDialogFragment.TITLE to "作业清单",
                    ClassHomeworkDetailDialog.DATA to item,
                    BasicDialogFragment.CLOSE to true
                )
            }
                .show(childFragmentManager, "CHDD")
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        //
        fun updateWeekType(pointTo: Int) {
            if (pointTo == 0) {
                currentWeekType = 0
            } else {
                currentWeekType += pointTo
            }
            binding.tvWeek.text =  if (currentWeekType == 0) getString(R.string.class_current_week) else "回到本周"
            viewModel.loadData(currentWeekType)
        }

        binding.ivLeft.setOnClickListener {
            updateWeekType(-1)
        }
        binding.ivRight.setOnClickListener {
            updateWeekType(1)
        }
        binding.tvWeek.setOnClickListener {
            updateWeekType(0)
        }
        binding.search.doOnSearch {
//            viewModel.load(text = it)
            adapter.notifyDataSetChanged()
            false
        }

        binding.statistical.setOnClickListener {
            CommonActivity.startActivity(requireContext(), ClassHomeworkStatisticalFragment::class.java)
        }
        binding.classSubmitHomework.setOnClickListener {
            val intent = Intent(requireActivity(), AttestationActivity::class.java)
            val list = adapter.list
            if (list.isNotEmpty()) {
//                ToastUtil.showToast(requireActivity(), "当前选择的日期没有需要提交的作业")
//                return@setOnClickListener
                intent.putExtra(EXTRA_DATA, ArrayList(list))
            }
            startActivityForResult(intent, AttestationActivity.HOMEWORK)


//            //没有作业的情况下
//            startFragment(ClassSubmitHomeworkFragment::class.java, bundleOf())
        }

        binding.headerRecycler.adapter = titleAdapter

        binding.recycler.addItemDecoration(GridSpacingItemDecoration(7, 32, false))
        binding.recycler.adapter = adapter

        viewModel.weekData.observe(viewLifecycleOwner) {
            titleAdapter.notifyChanged(it)
        }
        viewModel.data.observe(viewLifecycleOwner) {
            adapter.notifyChanged(it)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        onResult(requestCode, resultCode, data)
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden.not() && view != null) {
            viewModel.loadData()
        }
    }
}
